﻿namespace WordCloudClient.ViewModels
{
    public class WinEditDictionaryViewModel : WinBaseVM
    {
        public WinEditDictionaryViewModel(SystemService systemService, WinEditHelper winEditHelper) : base(systemService, winEditHelper)
        {
        }
        private DictionaryEntity _dictionaryEntity = new();
        public DictionaryEntity DictionaryEntity
        {
            get { return _dictionaryEntity; }
            set
            {
                _dictionaryEntity = value;
                RaisePropertyChanged(nameof(DictionaryEntity));
            }
        }
        private bool codeIsRead = false;
        public bool CodeIsRead
        {
            get { return codeIsRead; }
            set
            {
                codeIsRead = value;
                RaisePropertyChanged(nameof(CodeIsRead));
            }
        }

        public override void ExecuteSave()
        {
            if (!CommonHelper.AttributeVerify(DictionaryEntity))
            {
                return;
            }
            bool res;
            string errorMsg = string.Empty;
            if (Title.Contains("新增"))
            {
                res = _systemService.AddDictionary(DictionaryEntity, ref errorMsg);
            }
            else
            {
                res = _systemService.UpdateDictionary(DictionaryEntity, ref errorMsg);
            }
            if (res)
            {
                RaiseRequestClose(new DialogResult(ButtonResult.OK));
            }
            else
            {
                MessageTips.ErrorMessage($"操作失败！{errorMsg}");
                return;
            }
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            string operationType = parameters.GetValue<string>("OperationType");
            if (operationType == "新增")
            {
                Title = "新增字典";
                DictionaryEntity = new();
            }
            else if (operationType == "编辑")
            {
                Title = "编辑字典";
                DictionaryEntity = parameters.GetValue<DictionaryEntity>("FristEntity");
                CodeIsRead = true;
            }
        }
    }
}
